﻿namespace TriangleGames.TrainResEditor
{
    using TriangleGames.TrainRes;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using UnityEditor;
    using UnityEngine;

    public class ActionEditor
    {
        private bool actionIsDirty;
        private int arrayControlID;
        private static MonoBehaviour behaviourContext;
        private readonly List<FieldInfo> compoundArrayChild = new List<FieldInfo>();
        private readonly List<CompoundArrayAttribute> compoundArrayList = new List<CompoundArrayAttribute>();
        private readonly List<FieldInfo> compoundArrayParent = new List<FieldInfo>();
        private FsmStateAction editingAction;
        private string editingArray;
        private FsmStateAction editingArrayInAction;
        private static FsmEventTarget fsmEventTargetContext;
        public static FsmEventTarget FSMEventTargetContextGlobal;
        private static string fsmNameContext;
        public static GameObject GameObjectContext;
        private FsmStateAction initAction;
        private bool openParent;
        private string realActionName;
        private int tempArraySize;

        private Array EditArray(GUIContent label, System.Type elementType, object fieldValue, object[] attributes)
        {
            Array array;
            if (fieldValue != null)
            {
                array = (Array) fieldValue;
            }
            else
            {
                array = Array.CreateInstance(elementType, 0);
            }
            bool changed = GUI.changed;
            if ((this.editingArrayInAction == this.editingAction) && (this.editingArray == label.text))
            {
                this.tempArraySize = EditorGUILayout.IntField(label, this.tempArraySize, new GUILayoutOption[0]);
                if (GUIUtility.keyboardControl != this.arrayControlID)
                {
                    FsmEditor.RegisterUndo("调整数组大小");
                    this.editingArray = "";
                    if (this.tempArraySize != array.Length)
                    {
                        if (this.tempArraySize < 0)
                        {
                            this.tempArraySize = 0;
                        }
                        Array array2 = Array.CreateInstance(elementType, this.tempArraySize);
                        int num = Math.Min(this.tempArraySize, array.Length);
                        for (int j = 0; j < num; j++)
                        {
                            array2.SetValue(array.GetValue(j), j);
                        }
                        this.actionIsDirty = true;
                        return array2;
                    }
                }
                GUI.changed = changed;
            }
            else
            {
                int num3 = EditorGUILayout.IntField(label, array.Length, new GUILayoutOption[0]);
                if (num3 != array.Length)
                {
                    this.editingArrayInAction = this.editingAction;
                    this.arrayControlID = GUIUtility.keyboardControl;
                    this.editingArray = label.text;
                    this.tempArraySize = num3;
                }
            }
            GUI.changed = changed;
            for (int i = 0; i < array.Length; i++)
            {
                array.SetValue(this.GUIForFieldTypes("  元素 " + i, elementType, array.GetValue(i), attributes), i);
            }
            FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
            if (GUI.changed)
            {
                this.actionIsDirty = true;
            }
            return array;
        }

        public static string EditComponentName(GUIContent label, string name, System.Type componentType)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            name = EditorGUILayout.TextField(label, name, new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            string str = FsmEditorGUILayout.ComponentNamePopup(GameObjectContext, componentType);
            if (GUI.changed)
            {
                name = str;
            }
            else
            {
                GUI.changed = changed;
            }
            GUILayout.EndHorizontal();
            return name;
        }

        private void EditCompoundArray(FsmStateAction action, int compoundArrayIndex)
        {
            GUIContent label = new GUIContent(this.compoundArrayList[compoundArrayIndex].Name);
            FieldInfo info = this.compoundArrayParent[compoundArrayIndex];
            System.Type fieldType = info.FieldType;
            object obj2 = info.GetValue(action);
            System.Type elementType = fieldType.GetElementType();
            object[] customAttributes = info.GetCustomAttributes(true);
            string firstArrayName = this.compoundArrayList[compoundArrayIndex].FirstArrayName;
            if (elementType != null)
            {
                Array array;
                if (obj2 != null)
                {
                    array = (Array) obj2;
                }
                else
                {
                    array = Array.CreateInstance(elementType, 0);
                }
                FieldInfo info2 = this.compoundArrayChild[compoundArrayIndex];
                System.Type type3 = info2.FieldType;
                object obj3 = info2.GetValue(action);
                System.Type type4 = type3.GetElementType();
                object[] attributes = info2.GetCustomAttributes(true);
                string secondArrayName = this.compoundArrayList[compoundArrayIndex].SecondArrayName;
                if (type4 != null)
                {
                    Array array2;
                    if (obj3 != null)
                    {
                        array2 = (Array) obj3;
                    }
                    else
                    {
                        array2 = Array.CreateInstance(type4, 0);
                    }
                    bool changed = GUI.changed;
                    if ((this.editingArrayInAction == this.editingAction) && (this.editingArray == info.Name))
                    {
                        this.tempArraySize = EditorGUILayout.IntField(label, this.tempArraySize, new GUILayoutOption[0]);
                        if ((GUIUtility.keyboardControl != this.arrayControlID) && (this.tempArraySize != array.Length))
                        {
                            FsmEditor.RegisterUndo("调整复合数组大小");
                            this.editingArray = "";
                            if (this.tempArraySize < 0)
                            {
                                this.tempArraySize = 0;
                            }
                            Array array3 = Array.CreateInstance(elementType, this.tempArraySize);
                            int num = Math.Min(this.tempArraySize, array.Length);
                            for (int j = 0; j < num; j++)
                            {
                                array3.SetValue(array.GetValue(j), j);
                            }
                            Array array4 = Array.CreateInstance(type4, this.tempArraySize);
                            for (int k = 0; k < num; k++)
                            {
                                array4.SetValue(array2.GetValue(k), k);
                            }
                            info.SetValue(action, array3);
                            info2.SetValue(action, array4);
                            this.actionIsDirty = true;
                            return;
                        }
                    }
                    else
                    {
                        int num4 = EditorGUILayout.IntField(label, array.Length, new GUILayoutOption[0]);
                        if (num4 != array.Length)
                        {
                            this.editingArrayInAction = this.editingAction;
                            this.arrayControlID = GUIUtility.keyboardControl;
                            this.editingArray = info.Name;
                            this.tempArraySize = num4;
                        }
                    }
                    GUI.changed = changed;
                    if (array.Length > 0)
                    {
                        FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                    }
                    for (int i = 0; i < array2.Length; i++)
                    {
                        try
                        {
                            array.SetValue(this.GUIForFieldTypes(" " + firstArrayName, elementType, array.GetValue(i), customAttributes), i);
                        }
                        catch
                        {
                            GUIUtility.ExitGUI();
                            return;
                        }
                        array2.SetValue(this.GUIForFieldTypes(" " + secondArrayName, type4, array2.GetValue(i), attributes), i);
                        FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                    }
                    if (GUI.changed)
                    {
                        info.SetValue(action, array);
                        info2.SetValue(action, array2);
                        this.actionIsDirty = true;
                    }
                }
            }
        }

        private static float EditFloatValue(GUIContent label, object fieldValue, object[] attributes)
        {
            HasFloatSliderAttribute floatSliderAttribute = GetFloatSliderAttribute(attributes);
            if (floatSliderAttribute == null)
            {
                return EditorGUILayout.FloatField(label, (float) fieldValue, new GUILayoutOption[0]);
            }
            return FsmEditorGUILayout.FloatSlider(label, (float) fieldValue, floatSliderAttribute.MinValue, floatSliderAttribute.MaxValue);
        }

        private static FsmBool EditFsmBool(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmBool fsmBool = ((FsmBool) fieldValue) ?? new FsmBool("");
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmBoolField(label, FsmEditor.SelectedFsm, fsmBool);
            }
            return VariableEditor.FsmBoolPopup(label, FsmEditor.SelectedFsm, fsmBool);
        }

        private static FsmColor EditFsmColor(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmColor fsmColor = ((FsmColor) fieldValue) ?? new FsmColor();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmColorField(label, FsmEditor.SelectedFsm, fsmColor);
            }
            return VariableEditor.FsmColorPopup(label, FsmEditor.SelectedFsm, fsmColor);
        }

        public static string EditFsmEvent(GUIContent label, string name)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            name = EditorGUILayout.TextField(label, name, new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            string str = FsmEditorGUILayout.FsmEventPopup(GameObjectContext, fsmNameContext);
            if (GUI.changed)
            {
                name = str;
            }
            else
            {
                GUI.changed = changed;
            }
            GUILayout.EndHorizontal();
            return name;
        }

        private FsmEventTarget EditFsmEventTarget(object fieldValue)
        {
            FsmEventTarget target = ((FsmEventTarget) fieldValue) ?? new FsmEventTarget();
            FsmEventTarget.EventTarget target2 = (FsmEventTarget.EventTarget) EditorGUILayout.EnumPopup("事件目标", target.target, new GUILayoutOption[0]);
            if (target2 != target.target)
            {
                target.target = target2;
                target.ResetParameters();
            }
            if (this.realActionName == "设置事件目标")
            {
                FSMEventTargetContextGlobal = target;
            }
            else
            {
                fsmEventTargetContext = target;
            }
            switch (target.target)
            {
                case FsmEventTarget.EventTarget.Self:
                    return target;

                case FsmEventTarget.EventTarget.GameObject:
                    if (FsmEditorSettings.ShowHints)
                    {
                        GUILayout.Box("发送到对象的全部状态机.\n注意: 事件必须是全局事件.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                    }
                    target.gameObject = EditFsmOwnerDefault(new GUIContent("对象", "发送事件到这个对象的全部状态机."), target.gameObject);
                    target.sendToChildren = VariableEditor.FsmBoolField(new GUIContent("发送到子对象", "发送事件到全部子对象."), FsmEditor.SelectedFsm, target.sendToChildren);
                    return target;

                case FsmEventTarget.EventTarget.GameObjectFSM:
                    if (FsmEditorSettings.ShowHints)
                    {
                        GUILayout.Box("发送事件到对象的特定状态机.\n注意: 事件必须是全局事件.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                    }
                    target.gameObject = EditFsmOwnerDefault(new GUIContent("对象", "发送事件到对象的特定状态机."), target.gameObject);
                    target.fsmName = VariableEditor.FsmStringField(new GUIContent("状态机名称", "如果对象有多个状态机，填写状态机的名称."), FsmEditor.SelectedFsm, target.fsmName, new object[] { new UIHintAttribute(UIHint.FsmName) });
                    return target;

                case FsmEventTarget.EventTarget.FSMComponent:
                    if (FsmEditorSettings.ShowHints)
                    {
                        GUILayout.Box("发送事件到TrainResFSM组件. 拖放组件到这里.\n注意: 事件必须是全局事件.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                    }
                    target.fsmComponent = (TrainResFSMCore)EditorGUILayout.ObjectField(new GUIContent("状态机组件", "发送事件到TrainResFSM组件."), target.fsmComponent, typeof(TrainResFSMCore), !FsmEditor.SelectedFsmIsPrefab(), new GUILayoutOption[0]);
                    FsmEditorGUILayout.ReadonlyTextField((target.fsmComponent == null) ? "" : target.fsmComponent.Fsm.Name, new GUILayoutOption[0]);
                    return target;

                case FsmEventTarget.EventTarget.BroadcastAll:
                    if (FsmEditorSettings.ShowHints)
                    {
                        GUILayout.Box("广播事件到全部状态机.\n注意: 事件必须是全局事件.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                    }
                    target.excludeSelf = VariableEditor.FsmBoolField(new GUIContent("排除自身"), FsmEditor.SelectedFsm, target.excludeSelf);
                    return target;
            }
            return target;
        }

        private static FsmFloat EditFsmFloat(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmFloat fsmFloat = ((FsmFloat) fieldValue) ?? new FsmFloat(string.Empty);
            if (GetUIHint(attributes) == UIHint.Variable)
            {
                return VariableEditor.FsmFloatPopup(label, FsmEditor.SelectedFsm, fsmFloat);
            }
            HasFloatSliderAttribute floatSliderAttribute = GetFloatSliderAttribute(attributes);
            if (floatSliderAttribute == null)
            {
                return VariableEditor.FsmFloatField(label, FsmEditor.SelectedFsm, fsmFloat);
            }
            return VariableEditor.FsmFloatSlider(label, FsmEditor.SelectedFsm, fsmFloat, floatSliderAttribute.MinValue, floatSliderAttribute.MaxValue);
        }

        private static FsmGameObject EditFsmGameObject(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmGameObject fsmGameObject = ((FsmGameObject) fieldValue) ?? new FsmGameObject(string.Empty);
            if (GetUIHint(attributes) == UIHint.Variable)
            {
                return VariableEditor.FsmGameObjectPopup(label, FsmEditor.SelectedFsm, fsmGameObject);
            }
            fsmGameObject = VariableEditor.FsmGameObjectField(label, FsmEditor.SelectedFsm, fsmGameObject);
            if (fsmGameObject.Value != null)
            {
                GameObjectContext = fsmGameObject.Value;
            }
            return fsmGameObject;
        }

        private static FsmInt EditFsmInt(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmInt fsmInt = ((FsmInt) fieldValue) ?? new FsmInt("");
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmIntField(label, FsmEditor.SelectedFsm, fsmInt, attributes);
            }
            return VariableEditor.FsmIntPopup(label, FsmEditor.SelectedFsm, fsmInt);
        }

        private static FsmMaterial EditFsmMaterial(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmMaterial fsmObject = ((FsmMaterial) fieldValue) ?? new FsmMaterial();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmMaterialField(label, FsmEditor.SelectedFsm, fsmObject);
            }
            return VariableEditor.FsmMaterialPopup(label, FsmEditor.SelectedFsm, fsmObject);
        }

        public static string EditFsmName(GUIContent label, string name)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            name = EditorGUILayout.TextField(label, name, new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            string str = FsmEditorGUILayout.FsmNamePopup(GameObjectContext);
            if (GUI.changed)
            {
                name = str;
            }
            else
            {
                GUI.changed = changed;
            }
            GUILayout.EndHorizontal();
            fsmNameContext = name;
            return name;
        }

        private static FsmObject EditFsmObject(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmObject fsmObject = ((FsmObject) fieldValue) ?? new FsmObject();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmObjectField(label, FsmEditor.SelectedFsm, fsmObject, GetObjectType(attributes));
            }
            return VariableEditor.FsmObjectPopup(label, FsmEditor.SelectedFsm, fsmObject, GetObjectType(attributes));
        }

        private static FsmOwnerDefault EditFsmOwnerDefault(GUIContent label, object fieldValue)
        {
            FsmOwnerDefault default2 = ((FsmOwnerDefault) fieldValue) ?? new FsmOwnerDefault();
            default2.OwnerOption = (OwnerDefaultOption) EditorGUILayout.EnumPopup(label, default2.OwnerOption, new GUILayoutOption[0]);
            if (default2.OwnerOption == OwnerDefaultOption.SpecifyGameObject)
            {
                default2.GameObject = VariableEditor.FsmGameObjectField(new GUIContent("  "), FsmEditor.SelectedFsm, default2.GameObject);
                if (default2.GameObject.Value != null)
                {
                    GameObjectContext = default2.GameObject.Value;
                }
                return default2;
            }
            if (FsmEditor.SelectedFsm != null)
            {
                GameObjectContext = FsmEditor.SelectedFsm.GameObject;
            }
            return default2;
        }

        private static FsmProperty EditFsmProperty(object fieldValue, object[] attributes)
        {
            FsmProperty fsmProperty = ((FsmProperty) fieldValue) ?? new FsmProperty();
            fsmProperty.Init();
            bool changed = GUI.changed;
            GUI.changed = false;
            fsmProperty.TargetObject = EditFsmObject(new GUIContent("目标对象", "选择一个目标对象"), fsmProperty.TargetObject, attributes);
            if (GUI.changed || (fsmProperty.prevTargetObject != fsmProperty.TargetObject.Value))
            {
                fsmProperty.prevTargetObject = fsmProperty.TargetObject.Value;
                fsmProperty.TargetTypeName = (fsmProperty.TargetObject.Value != null) ? fsmProperty.TargetObject.Value.GetType().ToString() : null;
                if (fsmProperty.TargetObject.Value == null)
                {
                    fsmProperty.TargetTypeName = null;
                }
                fsmProperty.PropertyName = "";
                fsmProperty.PropertyType = null;
                fsmProperty.ResetParameters();
            }
            if (!GUI.changed)
            {
                GUI.changed = changed;
            }
            if (((fsmProperty.TargetObject.Value == null) && !fsmProperty.TargetObject.UseVariable) || fsmProperty.TargetObject.IsNone)
            {
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                return fsmProperty;
            }
            if (string.IsNullOrEmpty(fsmProperty.TargetTypeName))
            {
                if (fsmProperty.TargetObject.UseVariable)
                {
                    fsmProperty.TargetTypeName = fsmProperty.TargetObject.TypeName;
                    fsmProperty.TargetType = fsmProperty.TargetObject.ObjectType;
                }
                else if (fsmProperty.TargetObject.Value != null)
                {
                    fsmProperty.TargetType = fsmProperty.TargetObject.Value.GetType();
                    fsmProperty.TargetTypeName = fsmProperty.TargetType.FullName;
                }
            }
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Space(6f);
            GUILayout.Label("对象类型", new GUILayoutOption[] { GUILayout.MaxWidth(144f) });
            FsmEditorGUILayout.ReadonlyTextField(fsmProperty.TargetTypeName, new GUILayoutOption[0]);
            GUILayout.EndHorizontal();
            string text = "空";
            if (!string.IsNullOrEmpty(fsmProperty.PropertyName))
            {
                text = fsmProperty.PropertyName;
            }
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Space(6f);
            GUILayout.Label(new GUIContent("属性"), new GUILayoutOption[] { GUILayout.MaxWidth(144f) });
            if (GUILayout.Button(new GUIContent(text), EditorStyles.popup, new GUILayoutOption[0]))
            {
                FsmEditorUtility.GeneratePropertyMenu(fsmProperty).ShowAsContext();
            }
            GUILayout.EndHorizontal();
            if (string.IsNullOrEmpty(fsmProperty.PropertyName))
            {
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                return fsmProperty;
            }
            if (fsmProperty.PropertyType == null)
            {
                if (!string.IsNullOrEmpty(fsmProperty.PropertyName))
                {
                    fsmProperty.SetPropertyName(fsmProperty.PropertyName);
                }
                if (fsmProperty.PropertyType == null)
                {
                    FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                    return fsmProperty;
                }
            }
            if (fsmProperty.setProperty)
            {
                if (fsmProperty.PropertyType.IsAssignableFrom(typeof(bool)))
                {
                    fsmProperty.BoolParameter = VariableEditor.FsmBoolField(new GUIContent("布尔"), FsmEditor.SelectedFsm, fsmProperty.BoolParameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(float)))
                {
                    fsmProperty.FloatParameter = VariableEditor.FsmFloatField(new GUIContent("浮点"), FsmEditor.SelectedFsm, fsmProperty.FloatParameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(int)))
                {
                    fsmProperty.IntParameter = VariableEditor.FsmIntField(new GUIContent("整型"), FsmEditor.SelectedFsm, fsmProperty.IntParameter, null);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(GameObject)))
                {
                    fsmProperty.GameObjectParameter = VariableEditor.FsmGameObjectField(new GUIContent("游戏对象"), FsmEditor.SelectedFsm, fsmProperty.GameObjectParameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(string)))
                {
                    fsmProperty.StringParameter = VariableEditor.FsmStringField(new GUIContent("字符串"), FsmEditor.SelectedFsm, fsmProperty.StringParameter, null);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Vector3)))
                {
                    fsmProperty.Vector3Parameter = VariableEditor.FsmVector3Field(new GUIContent("向量"), FsmEditor.SelectedFsm, fsmProperty.Vector3Parameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Rect)))
                {
                    fsmProperty.RectParamater = VariableEditor.FsmRectField(new GUIContent("四边形"), FsmEditor.SelectedFsm, fsmProperty.RectParamater);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Material)))
                {
                    fsmProperty.MaterialParameter = VariableEditor.FsmMaterialField(new GUIContent("材质"), FsmEditor.SelectedFsm, fsmProperty.MaterialParameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Texture)))
                {
                    fsmProperty.TextureParameter = VariableEditor.FsmTextureField(new GUIContent("贴图"), FsmEditor.SelectedFsm, fsmProperty.TextureParameter);
                }
                else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Quaternion)))
                {
                    fsmProperty.QuaternionParameter = VariableEditor.FsmQuaternionField(new GUIContent("四元数"), FsmEditor.SelectedFsm, fsmProperty.QuaternionParameter);
                }
                else if (fsmProperty.PropertyType.IsSubclassOf(typeof(UnityEngine.Object)))
                {
                    fsmProperty.ObjectParameter = VariableEditor.FsmObjectField(new GUIContent("对象"), FsmEditor.SelectedFsm, fsmProperty.ObjectParameter, fsmProperty.PropertyType);
                }
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(bool)))
            {
                fsmProperty.BoolParameter = VariableEditor.FsmBoolPopup(new GUIContent("存储到 布尔"), FsmEditor.SelectedFsm, fsmProperty.BoolParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(float)))
            {
                fsmProperty.FloatParameter = VariableEditor.FsmFloatPopup(new GUIContent("存储到 浮点"), FsmEditor.SelectedFsm, fsmProperty.FloatParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(int)))
            {
                fsmProperty.IntParameter = VariableEditor.FsmIntPopup(new GUIContent("存储到 整型"), FsmEditor.SelectedFsm, fsmProperty.IntParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(GameObject)))
            {
                fsmProperty.GameObjectParameter = VariableEditor.FsmGameObjectPopup(new GUIContent("存储到 游戏对象"), FsmEditor.SelectedFsm, fsmProperty.GameObjectParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(string)))
            {
                fsmProperty.StringParameter = VariableEditor.FsmStringPopup(new GUIContent("存储到 字符串"), FsmEditor.SelectedFsm, fsmProperty.StringParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Vector3)))
            {
                fsmProperty.Vector3Parameter = VariableEditor.FsmVector3Popup(new GUIContent("存储到 向量"), FsmEditor.SelectedFsm, fsmProperty.Vector3Parameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Rect)))
            {
                fsmProperty.RectParamater = VariableEditor.FsmRectPopup(new GUIContent("存储到 四边形"), FsmEditor.SelectedFsm, fsmProperty.RectParamater);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Material)))
            {
                fsmProperty.MaterialParameter = VariableEditor.FsmMaterialPopup(new GUIContent("存储到 材质"), FsmEditor.SelectedFsm, fsmProperty.MaterialParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Texture)))
            {
                fsmProperty.TextureParameter = VariableEditor.FsmTexturePopup(new GUIContent("存储到 贴图"), FsmEditor.SelectedFsm, fsmProperty.TextureParameter);
            }
            else if (fsmProperty.PropertyType.IsAssignableFrom(typeof(Quaternion)))
            {
                fsmProperty.QuaternionParameter = VariableEditor.FsmQuaternionPopup(new GUIContent("存储到 四元数"), FsmEditor.SelectedFsm, fsmProperty.QuaternionParameter);
            }
            else if (fsmProperty.PropertyType.IsSubclassOf(typeof(UnityEngine.Object)))
            {
                fsmProperty.ObjectParameter = VariableEditor.FsmObjectPopup(new GUIContent("存储到 对象"), FsmEditor.SelectedFsm, fsmProperty.ObjectParameter, fsmProperty.PropertyType);
            }
            FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
            return fsmProperty;
        }

        private static FsmQuaternion EditFsmQuaternion(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmQuaternion fsmQauternion = ((FsmQuaternion) fieldValue) ?? new FsmQuaternion();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmQuaternionField(label, FsmEditor.SelectedFsm, fsmQauternion);
            }
            return VariableEditor.FsmQuaternionPopup(label, FsmEditor.SelectedFsm, fsmQauternion);
        }

        private static FsmRect EditFsmRect(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmRect fsmRect = ((FsmRect) fieldValue) ?? new FsmRect();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmRectField(label, FsmEditor.SelectedFsm, fsmRect);
            }
            return VariableEditor.FsmRectPopup(label, FsmEditor.SelectedFsm, fsmRect);
        }

        private static FsmString EditFsmString(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmString fsmString = ((FsmString) fieldValue) ?? new FsmString();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmStringField(label, FsmEditor.SelectedFsm, fsmString, attributes);
            }
            return VariableEditor.FsmStringPopup(label, FsmEditor.SelectedFsm, fsmString);
        }

        private static FsmTexture EditFsmTexture(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmTexture fsmObject = ((FsmTexture) fieldValue) ?? new FsmTexture();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmTextureField(label, FsmEditor.SelectedFsm, fsmObject);
            }
            return VariableEditor.FsmTexturePopup(label, FsmEditor.SelectedFsm, fsmObject);
        }

        public static string EditFsmVariableName(GUIContent label, string name, UIHint hint)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            name = EditorGUILayout.TextField(label, name, new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            string str = FsmEditorGUILayout.FsmVariablePopup(GameObjectContext, fsmNameContext, hint);
            if (GUI.changed)
            {
                name = str;
            }
            else
            {
                GUI.changed = changed;
            }
            GUILayout.EndHorizontal();
            return name;
        }

        private static FsmVector3 EditFsmVector3(GUIContent label, object fieldValue, object[] attributes)
        {
            FsmVector3 vector = ((FsmVector3) fieldValue) ?? new FsmVector3();
            if (GetUIHint(attributes) != UIHint.Variable)
            {
                return VariableEditor.FsmVector3Field(label, FsmEditor.SelectedFsm, vector);
            }
            return VariableEditor.FsmVector3Popup(label, FsmEditor.SelectedFsm, vector);
        }

        private static FunctionCall EditFunctionCall(object fieldValue, object[] attributes)
        {
            FunctionCall call = ((FunctionCall) fieldValue) ?? new FunctionCall();
            bool coroutinesOnly = GetUIHint(attributes) == UIHint.Coroutine;
            call.FunctionName = EditMethodName(new GUIContent("方法名称"), call.FunctionName, coroutinesOnly);
            string str = FsmEditorGUILayout.ParameterTypePopup("参数", call.ParameterType);
            if (str != call.ParameterType)
            {
                call.ParameterType = str;
                call.ResetParameters();
            }
            switch (call.ParameterType)
            {
                case "int":
                    call.IntParameter = VariableEditor.FsmIntField(new GUIContent("整型"), FsmEditor.SelectedFsm, call.IntParameter, attributes);
                    return call;

                case "float":
                    call.FloatParameter = VariableEditor.FsmFloatField(new GUIContent("浮点"), FsmEditor.SelectedFsm, call.FloatParameter);
                    return call;

                case "string":
                    call.StringParameter = VariableEditor.FsmStringField(new GUIContent("字符串"), FsmEditor.SelectedFsm, call.StringParameter, attributes);
                    return call;

                case "Vector3":
                    call.Vector3Parameter = VariableEditor.FsmVector3Field(new GUIContent("向量"), FsmEditor.SelectedFsm, call.Vector3Parameter);
                    return call;

                case "Rect":
                    call.RectParamater = VariableEditor.FsmRectField(new GUIContent("四边形"), FsmEditor.SelectedFsm, call.RectParamater);
                    return call;

                case "bool":
                    call.BoolParameter = VariableEditor.FsmBoolField(new GUIContent("布尔"), FsmEditor.SelectedFsm, call.BoolParameter);
                    return call;

                case "GameObject":
                    call.GameObjectParameter = VariableEditor.FsmGameObjectField(new GUIContent("游戏对象"), FsmEditor.SelectedFsm, call.GameObjectParameter);
                    return call;

                case "Material":
                    call.MaterialParameter = VariableEditor.FsmMaterialField(new GUIContent("材质"), FsmEditor.SelectedFsm, call.MaterialParameter);
                    return call;

                case "Texture":
                    call.TextureParameter = VariableEditor.FsmTextureField(new GUIContent("贴图"), FsmEditor.SelectedFsm, call.TextureParameter);
                    return call;

                case "Quaternion":
                    call.QuaternionParameter = VariableEditor.FsmQuaternionField(new GUIContent("四元数"), FsmEditor.SelectedFsm, call.QuaternionParameter);
                    return call;

                case "Object":
                    call.ObjectParameter = VariableEditor.FsmObjectField(new GUIContent("对象"), FsmEditor.SelectedFsm, call.ObjectParameter, typeof(UnityEngine.Object));
                    return call;
            }
            return call;
        }

        private static int EditIntValue(GUIContent label, object fieldValue, object[] attributes)
        {
            if (GetUIHint(attributes) != UIHint.Layer)
            {
                return EditorGUILayout.IntField(label, Convert.ToInt32(fieldValue, CultureInfo.CurrentCulture), new GUILayoutOption[0]);
            }
            return EditorGUILayout.LayerField(label, Convert.ToInt32(fieldValue, CultureInfo.CurrentCulture), new GUILayoutOption[0]);
        }

        private static LayoutOption EditLayoutOption(object fieldValue)
        {
            LayoutOption option = ((LayoutOption) fieldValue) ?? new LayoutOption();
            option.option = (LayoutOption.LayoutOptionType) EditorGUILayout.EnumPopup(" 选项", option.option, new GUILayoutOption[0]);
            switch (option.option)
            {
                case LayoutOption.LayoutOptionType.Width:
                case LayoutOption.LayoutOptionType.Height:
                case LayoutOption.LayoutOptionType.MinWidth:
                case LayoutOption.LayoutOptionType.MaxWidth:
                case LayoutOption.LayoutOptionType.MinHeight:
                case LayoutOption.LayoutOptionType.MaxHeight:
                    option.floatParam = VariableEditor.FsmFloatField(new GUIContent(""), FsmEditor.SelectedFsm, option.floatParam);
                    return option;

                case LayoutOption.LayoutOptionType.ExpandWidth:
                case LayoutOption.LayoutOptionType.ExpandHeight:
                    option.boolParam = VariableEditor.FsmBoolField(new GUIContent(""), FsmEditor.SelectedFsm, option.boolParam);
                    return option;
            }
            return option;
        }

        public static string EditMethodName(GUIContent label, string name, bool coroutinesOnly)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            name = EditorGUILayout.TextField(label, name, new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            string str = FsmEditorGUILayout.MethodNamePopup(GameObjectContext, behaviourContext, 1, coroutinesOnly);
            if (GUI.changed)
            {
                name = str;
            }
            else
            {
                GUI.changed = changed;
            }
            GUILayout.EndHorizontal();
            return name;
        }

        private static string EditStringValue(GUIContent label, object fieldValue, object[] attributes)
        {
            string text = ((string) fieldValue) ?? string.Empty;
            switch (GetUIHint(attributes))
            {
                case UIHint.TextArea:
                    GUILayout.Label(label, new GUILayoutOption[0]);
                    return EditorGUILayout.TextArea(text, FsmEditorStyles.TextArea, new GUILayoutOption[] { GUILayout.MinHeight(44f) });

                case UIHint.Behaviour:
                    return EditComponentName(label, text, typeof(Behaviour));

                case UIHint.Script:
                    text = EditComponentName(label, text, typeof(MonoBehaviour));
                    TrySetBehaviourContext(text);
                    return text;

                case UIHint.Method:
                    return EditMethodName(label, text, false);

                case UIHint.Animation:
                {
                    GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                    text = EditorGUILayout.TextField(label, text, new GUILayoutOption[0]);
                    bool changed = GUI.changed;
                    GUI.changed = false;
                    string str2 = FsmEditorGUILayout.AnimationNamePopup(GameObjectContext);
                    if (GUI.changed && (str2 != ""))
                    {
                        text = str2;
                    }
                    if (!GUI.changed)
                    {
                        GUI.changed = changed;
                    }
                    GUILayout.EndHorizontal();
                    return text;
                }
                case UIHint.Tag:
                    return EditorGUILayout.TagField(label, text, new GUILayoutOption[0]);

                case UIHint.Description:
                    GUILayout.Box(text, FsmEditorStyles.InfoBox, new GUILayoutOption[0]);
                    return text;

                case UIHint.ScriptComponent:
                    return FsmEditorGUILayout.ScriptListPopup(label, text);

                case UIHint.Comment:
                    return EditorGUILayout.TextArea(text, FsmEditorStyles.TextArea, new GUILayoutOption[] { GUILayout.MinHeight(44f) });
            }
            return EditorGUILayout.TextField(label, text, new GUILayoutOption[0]);
        }

        private static FsmEvent FsmEventPopup(GUIContent label, FsmEvent fsmEvent)
        {
            if (fsmEventTargetContext != null)
            {
                return FsmEventPopup(label, fsmEvent, fsmEventTargetContext);
            }
            if (FSMEventTargetContextGlobal != null)
            {
                return FsmEventPopup(label, fsmEvent, FSMEventTargetContextGlobal);
            }
            return FsmEditorGUILayout.EventPopup(label, (FsmEditor.SelectedFsm != null) ? new List<FsmEvent>(FsmEditor.SelectedFsm.Events) : FsmEvent.EventList, fsmEvent);
        }

        private static FsmEvent FsmEventPopup(GUIContent label, FsmEvent fsmEvent, FsmEventTarget eventTarget)
        {
            switch (eventTarget.target)
            {
                case FsmEventTarget.EventTarget.Self:
                    return FsmEditorGUILayout.EventPopup(label, (FsmEditor.SelectedFsm != null) ? new List<FsmEvent>(FsmEditor.SelectedFsm.Events) : FsmEvent.EventList, fsmEvent);

                case FsmEventTarget.EventTarget.GameObject:
                {
                    GameObject ownerDefaultTarget = FsmEditor.SelectedFsm.GetOwnerDefaultTarget(eventTarget.gameObject);
                    if (ownerDefaultTarget == null)
                    {
                        return FsmEditorGUILayout.EventPopup(label, FsmEvent.EventList, fsmEvent);
                    }
                    return FsmEditorGUILayout.EventPopup(label, FsmEditorUtility.GetEventList(ownerDefaultTarget), fsmEvent);
                }
                case FsmEventTarget.EventTarget.GameObjectFSM:
                {
                    Fsm gameObjectFSM = FsmEditorUtility.GetGameObjectFSM(FsmEditor.SelectedFsm.GetOwnerDefaultTarget(eventTarget.gameObject), eventTarget.fsmName);
                    return FsmEditorGUILayout.EventPopup(label, FsmEditorUtility.GetEventList(gameObjectFSM), fsmEvent);
                }
                case FsmEventTarget.EventTarget.FSMComponent:
                    return FsmEditorGUILayout.EventPopup(label, FsmEditorUtility.GetEventList(eventTarget.fsmComponent), fsmEvent);

                case FsmEventTarget.EventTarget.BroadcastAll:
                    return FsmEditorGUILayout.EventPopup(label, FsmEditorUtility.GetGlobalEventList(), fsmEvent);
            }
            return FsmEditorGUILayout.EventPopup(label, FsmEvent.EventList, fsmEvent);
        }

        private static GameObject GameObjectField(GUIContent label, object fieldValue)
        {
            return (GameObject) EditorGUILayout.ObjectField(label, (GameObject) fieldValue, typeof(GameObject), !FsmEditor.SelectedFsmIsPrefab(), new GUILayoutOption[0]);
        }

        private static CompoundArrayAttribute GetCompoundArray(object[] attributes)
        {
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    CompoundArrayAttribute attribute = obj2 as CompoundArrayAttribute;
                    if (attribute != null)
                    {
                        return attribute;
                    }
                }
            }
            return null;
        }

        private static HasFloatSliderAttribute GetFloatSliderAttribute(object[] attributes)
        {
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    HasFloatSliderAttribute attribute = obj2 as HasFloatSliderAttribute;
                    if (attribute != null)
                    {
                        return attribute;
                    }
                }
            }
            return null;
        }

        public static System.Type GetObjectType(object[] attributes)
        {
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    ObjectTypeAttribute attribute = obj2 as ObjectTypeAttribute;
                    if (attribute != null)
                    {
                        return attribute.ObjectType;
                    }
                }
            }
            return typeof(UnityEngine.Object);
        }

        private static string GetTitle(object[] attributes)
        {
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    TitleAttribute attribute = obj2 as TitleAttribute;
                    if (attribute != null)
                    {
                        return attribute.Text;
                    }
                }
            }
            return null;
        }

        private static string GetTooltip(System.Type type, object[] attributes, string title)
        {
            string str = FsmEditorUtility.GetTypeTooltip(type);
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    TooltipAttribute attribute = obj2 as TooltipAttribute;
                    if (attribute != null)
                    {
                        return string.Format("{0}:{1}", attribute.Text, str);
                    }
                }
            }
            return title + ":" + str;
        }

        public static UIHint GetUIHint(object[] attributes)
        {
            if (attributes != null)
            {
                foreach (object obj2 in attributes)
                {
                    UIHintAttribute attribute = obj2 as UIHintAttribute;
                    if (attribute != null)
                    {
                        return attribute.Hint;
                    }
                }
            }
            return UIHint.None;
        }

        private object GUIForFieldTypes(string labelText, System.Type type, object fieldValue, object[] attributes)
        {
            string title = GetTitle(attributes);
            if (title != null)
            {
                labelText = title;
            }
            //GUIContent tooltipLabelContent = FsmEditorGUILayout.GetTooltipLabelContent(labelText, GetTooltip(type, attributes));
            GUIContent tooltipLabelContent = FsmEditorGUILayout.GetTooltipLabelContent(GetTooltip(type, attributes, labelText), labelText);
            if (type == typeof(int))
            {
                return EditIntValue(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(float))
            {
                return EditFloatValue(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(bool))
            {
                return EditorGUILayout.Toggle(tooltipLabelContent, (bool) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(string))
            {
                return EditStringValue(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(Vector2))
            {
                return EditorGUILayout.Vector2Field(labelText, (Vector2) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(Vector3))
            {
                return EditorGUILayout.Vector3Field(labelText, (Vector3) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(Vector4))
            {
                return EditorGUILayout.Vector4Field(labelText, (Vector4) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(Rect))
            {
                return EditorGUILayout.RectField(labelText, (Rect) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(FunctionCall))
            {
                return EditFunctionCall(fieldValue, attributes);
            }
            if (type == typeof(FsmEventTarget))
            {
                return this.EditFsmEventTarget(fieldValue);
            }
            if (type == typeof(LayoutOption))
            {
                return EditLayoutOption(fieldValue);
            }
            if (type == typeof(FsmFloat))
            {
                return EditFsmFloat(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmInt))
            {
                return EditFsmInt(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmBool))
            {
                return EditFsmBool(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmVector3))
            {
                return EditFsmVector3(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmRect))
            {
                return EditFsmRect(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmQuaternion))
            {
                return EditFsmQuaternion(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmObject))
            {
                return EditFsmObject(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmMaterial))
            {
                return EditFsmMaterial(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmTexture))
            {
                return EditFsmTexture(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmColor))
            {
                return EditFsmColor(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(FsmGameObject))
            {
                FsmGameObject obj2 = EditFsmGameObject(tooltipLabelContent, fieldValue, attributes);
                if (obj2.Value != null)
                {
                    GameObjectContext = obj2.Value;
                }
                return obj2;
            }
            if (type == typeof(FsmOwnerDefault))
            {
                return EditFsmOwnerDefault(tooltipLabelContent, fieldValue);
            }
            if (type == typeof(FsmEvent))
            {
                return FsmEventPopup(tooltipLabelContent, (FsmEvent) fieldValue);
            }
            if (type == typeof(FsmString))
            {
                return EditFsmString(tooltipLabelContent, fieldValue, attributes);
            }
            if (type == typeof(AnimationState))
            {
                return FsmEditorGUILayout.AnimationStatePopup(tooltipLabelContent, (AnimationState) fieldValue, GameObjectContext);
            }
            if (type == typeof(Behaviour))
            {
                return FsmEditorGUILayout.BehaviorPopup(tooltipLabelContent, (Behaviour) fieldValue, GameObjectContext);
            }
            if (type == typeof(MonoBehaviour))
            {
                return FsmEditorGUILayout.BehaviorPopup(tooltipLabelContent, (MonoBehaviour) fieldValue, GameObjectContext);
            }
            if (type == typeof(Color))
            {
                return EditorGUILayout.ColorField(tooltipLabelContent, (Color) fieldValue, new GUILayoutOption[0]);
            }
            if (type == typeof(FsmAnimationCurve))
            {
                FsmAnimationCurve curve = ((FsmAnimationCurve) fieldValue) ?? new FsmAnimationCurve();
                curve.curve = EditorGUILayout.CurveField(labelText, curve.curve, new GUILayoutOption[0]);
                return curve;
            }
            if (type == typeof(GameObject))
            {
                GameObject obj3 = GameObjectField(tooltipLabelContent, fieldValue);
                if (obj3 != null)
                {
                    GameObjectContext = obj3;
                }
                return obj3;
            }
            if (type.IsArray)
            {
                return this.EditArray(tooltipLabelContent, type.GetElementType(), fieldValue, attributes);
            }
            Enum selected = fieldValue as Enum;
            if (selected != null)//fsr
            {
                return EditorGUILayout.EnumPopup(tooltipLabelContent, selected, new GUILayoutOption[0]);
            }
            if (type.IsSubclassOf(typeof(UnityEngine.Object)) || (type == typeof(UnityEngine.Object)))
            {
                return EditorGUILayout.ObjectField(tooltipLabelContent.text, (UnityEngine.Object) fieldValue, type, !FsmEditor.SelectedFsmIsPrefab(), new GUILayoutOption[0]);
            }
            if (type == typeof(FsmProperty))
            {
                return EditFsmProperty(fieldValue, attributes);
            }
            GUILayout.Label("不支持类型: " + type, FsmEditorStyles.ErrorBox, new GUILayoutOption[0]);
            return fieldValue;
        }

        private static bool HideField(System.Type type, object fieldValue, object[] attributes)
        {
            if (!type.IsEnum)
            {
                bool flag = false;
                foreach (Attribute attribute in attributes)
                {
                    if (attribute is RequiredFieldAttribute)
                    {
                        return false;
                    }
                    if (attribute is UIHintAttribute)
                    {
                        UIHintAttribute attribute2 = attribute as UIHintAttribute;
                        flag = attribute2.Hint == UIHint.Variable;
                    }
                }
                if (type.IsSubclassOf(typeof(NamedVariable)))
                {
                    NamedVariable variable = (NamedVariable) fieldValue;
                    if (variable == null)
                    {
                        return true;
                    }
                    if (variable.UseVariable || flag)
                    {
                        return string.IsNullOrEmpty(variable.Name);
                    }
                    if (type == typeof(FsmGameObject))
                    {
                        return (((FsmGameObject) fieldValue).Value == null);
                    }
                    if (type == typeof(FsmString))
                    {
                        return string.IsNullOrEmpty(((FsmString) fieldValue).Value);
                    }
                    if (type == typeof(FsmTexture))
                    {
                        return (((FsmTexture) fieldValue).Value == null);
                    }
                    if (type == typeof(FsmMaterial))
                    {
                        return (((FsmMaterial) fieldValue).Value == null);
                    }
                    if (type == typeof(FsmObject))
                    {
                        return (((FsmObject) fieldValue).Value == null);
                    }
                }
                else
                {
                    if (type == typeof(FsmEvent))
                    {
                        FsmEvent fsmEvent = (FsmEvent) fieldValue;
                        return FsmEvent.IsNullOrEmpty(fsmEvent);
                    }
                    if (type.IsArray)
                    {
                        Array array = fieldValue as Array;
                        return ((array == null) || (array.Length == 0));
                    }
                }
            }
            return false;
        }

        private void InitAction(FsmStateAction action)
        {
            this.compoundArrayList.Clear();
            this.compoundArrayParent.Clear();
            this.compoundArrayChild.Clear();
            foreach (FieldInfo info in ActionData.GetFields(action.GetType()))
            {
                if (this.openParent)
                {
                    this.compoundArrayChild.Add(info);
                    this.openParent = false;
                }
                CompoundArrayAttribute compoundArray = GetCompoundArray(info.GetCustomAttributes(true));
                if (compoundArray != null)
                {
                    this.compoundArrayList.Add(compoundArray);
                    this.compoundArrayParent.Add(info);
                    this.openParent = true;
                }
            }
            this.realActionName = FsmEditorUtility.GetActionLabel(action);
            this.initAction = action;
        }

        public bool OnGUI(FsmStateAction action, [Optional, DefaultParameterValue(false)] bool debugVariables, [Optional, DefaultParameterValue(false)] bool isPreview)
        {
            if (action == null)
            {
                return false;
            }
            if (this.initAction != action)
            {
                this.InitAction(action);
            }
            VariableEditor.DebugVariables = debugVariables;
            this.actionIsDirty = false;
            GameObjectContext = null;
            behaviourContext = null;
            fsmEventTargetContext = null;
            this.editingAction = action;
            foreach (FieldInfo info in ActionData.GetFields(action.GetType()))
            {
                bool changed = GUI.changed;
                GUI.changed = false;
                System.Type fieldType = info.FieldType;
                object fieldValue = info.GetValue(action);
                object[] customAttributes = info.GetCustomAttributes(true);
                string labelText = FsmEditorUtility.NicifyVariableName(info.Name);
                List<FsmError> parameterErrors = FsmErrorChecker.GetParameterErrors(action, info.Name);
                FsmEditorGUILayout.ResetGUIColorsKeepAlpha();
                foreach (FsmError error in parameterErrors)
                {
                    if (error.Type == FsmError.ErrorType.requiredField)
                    {
                        labelText = labelText + "*";
                        GUI.backgroundColor = FsmEditorStyles.GuiBackgroundErrorColor;
                        StateInspector.ShowRequiredFieldFootnote = true;
                        break;
                    }
                }
                string fieldSection = FsmEditorUtility.GetFieldSection(customAttributes);
                if (fieldSection != null)
                {
                    FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                    if (fieldSection != string.Empty)
                    {
                        GUILayout.Label(fieldSection, EditorStyles.boldLabel, new GUILayoutOption[0]);
                    }
                }
                bool flag2 = (!isPreview && (FsmEditor.SelectedState.HideUnused || FsmEditorSettings.DimUnusedActionParameters)) && HideField(fieldType, fieldValue, customAttributes);
                if (!flag2 || !FsmEditor.SelectedState.HideUnused)
                {
                    Color color = GUI.color;
                    if (flag2 && FsmEditorSettings.DimUnusedActionParameters)
                    {
                        GUI.color = new Color(1f, 1f, 1f, GUI.color.a * 0.3f);
                    }
                    if (fieldType.IsArray)
                    {
                        if (!this.compoundArrayParent.Contains(info))
                        {
                            if (this.compoundArrayChild.Contains(info))
                            {
                                this.EditCompoundArray(action, this.compoundArrayChild.IndexOf(info));
                            }
                            else
                            {
                                info.SetValue(action, this.GUIForFieldTypes(labelText, fieldType, fieldValue, customAttributes));
                            }
                        }
                    }
                    else
                    {
                        info.SetValue(action, this.GUIForFieldTypes(labelText, fieldType, fieldValue, customAttributes));
                    }
                    foreach (FsmError error2 in parameterErrors)
                    {
                        if (error2.Type != FsmError.ErrorType.requiredField)
                        {
                            if (error2.Type == FsmError.ErrorType.missingRequiredComponent)
                            {
                                if (GUILayout.Button(error2.ErrorString + "\n[点击添加所需组件]", FsmEditorStyles.ActionErrorBox, new GUILayoutOption[0]) && (error2.GameObject != null))
                                {
                                    if (error2.ObjectType == typeof(UnityEngine.Renderer))
                                    {
                                        Undo.RegisterUndo(error2.GameObject, "添加组件: MeshRenderer");
                                        error2.GameObject.AddComponent<MeshRenderer>();
                                    }
                                    else if (error2.ObjectType.IsSubclassOf(typeof(Component)) && !error2.ObjectType.IsAbstract)
                                    {
                                        Undo.RegisterUndo(error2.GameObject, "添加组件: " + FsmEditorUtility.StripNamespace(error2.ObjectType.ToString()));
                                        error2.GameObject.AddComponent(error2.ObjectType);
                                    }
                                    else
                                    {
                                        Debug.LogError("不能添加组件: " + error2.ObjectType.Name);
                                    }
                                }
                            }
                            else
                            {
                                GUILayout.Box(error2.ErrorString, FsmEditorStyles.ActionErrorBox, new GUILayoutOption[0]);
                            }
                        }
                    }
                    if (GUI.changed)
                    {
                        this.actionIsDirty = true;
                    }
                    else
                    {
                        GUI.changed = changed;
                    }
                    GUI.color = color;
                }
            }
            EditorGUILayout.Space();
            return this.actionIsDirty;
        }

        public void Reset()
        {
            this.initAction = null;
        }

        public static void TrySetBehaviourContext(string behaviourName)
        {
            if (GameObjectContext != null)
            {
                behaviourContext = (MonoBehaviour) GameObjectContext.GetComponent(behaviourName);
            }
        }
    }
}

